package com.github.wxiaoqi.security.biz.modules.codex.biz;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dromara.hmily.annotation.Hmily;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.common.biz.request.CouponIntegralRefundReq;
import com.github.wxiaoqi.security.biz.common.biz.request.CouponIntegralUseReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingCouponFreezeReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingCouponRefundReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingCouponUnFreezeAndPayReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingCouponUnFreezeReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingFreezeReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingIntegralDetailListReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingIntegralDetailListRsp;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingIntegralFreezeReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingIntegralRefundReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingIntegralUnFreezeAndPayReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingIntegralUnFreezeReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingReceiveReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingRefundReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingUnFreezeAndPayReq;
import com.github.wxiaoqi.security.biz.common.biz.request.MarketingUnFreezeReq;
import com.github.wxiaoqi.security.biz.common.crm.request.FreezeRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.IntoAccountRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.UnFreezeAndOutAccountRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.UnFreezeRequest;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.biz.feign.CrmFeign;
import com.github.wxiaoqi.security.biz.modules.codex.entity.PersonalCouponRelation;
import com.github.wxiaoqi.security.biz.modules.codex.mapper.MarCouponMapper;
import com.github.wxiaoqi.security.biz.modules.codex.mapper.PersonalCouponRelationMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.entity.PersonalAccountEntity;
import com.github.wxiaoqi.security.common.enums.CustomerTypeEnums;
import com.github.wxiaoqi.security.common.exception.BusinessException;
import com.github.wxiaoqi.security.common.msg.AccountType;
import com.github.wxiaoqi.security.common.msg.BaseApiResponse;
import com.github.wxiaoqi.security.common.msg.CouponUseStatus;
import com.github.wxiaoqi.security.common.msg.EffectiveStatus;
import com.github.wxiaoqi.security.common.msg.MerchantType;
import com.github.wxiaoqi.security.common.msg.TradeType;
import com.github.wxiaoqi.security.common.util.EntityUtils;
import com.github.wxiaoqi.security.common.util.SerialNoUtil;

/**
 * 
 *
 * @author centerroot
 * @email ${email}
 * @date 2018-09-25 16:56:18
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class PersonalCouponRelationBiz extends BaseBiz<PersonalCouponRelationMapper, PersonalCouponRelation> {

	private final static Logger logger = LoggerFactory.getLogger(PersonalCouponRelationBiz.class);

	@Autowired
	private PersonalCouponRelationMapper personalCouponRelationMapper;
	@Autowired
	private MarCouponMapper couponMapper;
	@Autowired
	private CommonBiz commonBiz;
	@Autowired
	private CodexEngineBiz codexEngineBiz;
	@Autowired
	private CrmFeign crmFeign;

	/**
	 * 红包、积分领取整合方法 1、判断业务类型 2、红包领取，判断红包流水号平台编号是否存在，存在直接返成功，不存在存入一条用户红包关联信息
	 * 3、积分领取，查询用户积分账户是否存在，不存在创建账户并入账，存在直接入账，记录账务明细
	 * 
	 * @param marketingCouponReceiveReq
	 * @return
	 * @throws Exception 
	 */
	@Hmily(confirmMethod="marketingReceiveConfirm",cancelMethod="marketingReceiveCancel")
	public BaseApiResponse marketingReceive(MarketingReceiveReq marketingReceiveReq) throws Exception {
		Map<String, Object> beanToMap = EntityUtils.beanToMap(marketingReceiveReq);
		logger.info("红包、积分领取 try方法執行，请求参数：{}" + beanToMap);
		BaseApiResponse baseResponse = new BaseApiResponse();
		// 校验用户信息
		Map<String, Object> reqMap = new HashMap<>();
		reqMap.put("platformId", marketingReceiveReq.getPlatformId());
		reqMap.put("personalId", marketingReceiveReq.getPersonalId());
		Map<String, Object> respMap = crmFeign.queryPersonalInfo(reqMap);

		if (null == respMap || !BizResponseCode.OK.getCode().equals(respMap.get("code"))) {
			baseResponse.setCode(BizResponseCode.PER_NOTEXIST.getCode());
			baseResponse.setMessage(BizResponseCode.PER_NOTEXIST.getMessage());
			return baseResponse;
		}
		if ("10".equals(marketingReceiveReq.getBusinessType())) {
			// 红包
			baseResponse = couponReceive(marketingReceiveReq);
		} else if ("20".equals(marketingReceiveReq.getBusinessType())) {
			// 积分
			baseResponse = integralReceive(marketingReceiveReq);
		} else if ("30".equals(marketingReceiveReq.getBusinessType())) {
			// 红包和积分
			// 红包
			BaseApiResponse response = couponReceive(marketingReceiveReq);
			if (BizResponseCode.OK.getCode().equals(response.getCode())) {
				// 积分
				baseResponse = integralReceive(marketingReceiveReq);
			} else {
				return response;
			}
		}
		logger.info("红包、积分领取try响应参数：{}" + EntityUtils.beanToMap(baseResponse));
		return baseResponse;
	}
	
	
	/**
	 * 
	 * @param marketingReceiveReq
	 * @return
	 */
	public BaseApiResponse marketingReceiveConfirm(MarketingReceiveReq marketingReceiveReq) {
		logger.info("红包、积分领取 marketingReceiveConfirm方法執行，请求参数：{}" + marketingReceiveReq);
		return new BaseApiResponse();
	}
	
	/**
	 * 
	 * @param marketingReceiveReq
	 * @return
	 */
	public BaseApiResponse marketingReceiveCancel(MarketingReceiveReq marketingReceiveReq) {
		logger.info("红包、积分领取 marketingReceiveCancel方法執行，请求参数：{}" + marketingReceiveReq);
		return new BaseApiResponse();
	}

	/**
	 * 红包领取 红包领取，判断红包流水号平台编号是否存在，存在直接返成功，不存在存入一条用户红包关联信息
	 * 
	 * @param marketingCouponReceiveReq
	 * @return
	 */
	public BaseApiResponse couponReceive(MarketingReceiveReq marketingReceiveReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		Map<String, Object> beanToMap = EntityUtils.beanToMap(marketingReceiveReq);
		// List<CouponInfo> couponList = marketingReceiveReq.getCouponList();
		@SuppressWarnings("unchecked")
		List<Map<String, Object>> couponList = (List<Map<String, Object>>) beanToMap.get("couponList");
		// 遍历一张或多张红包
		if (couponList != null && couponList.size() > 0) {
			for (int i = 0; i < couponList.size(); i++) {
				PersonalCouponRelation personalCouponRelation = new PersonalCouponRelation();
				personalCouponRelation.setPlatformId(marketingReceiveReq.getPlatformId());
				personalCouponRelation.setPersonalId(marketingReceiveReq.getPersonalId());
				Map<String, Object> couponInfo = couponList.get(i);
				personalCouponRelation.setCouponSerialNo((String) couponInfo.get("couponSerialNo"));
				PersonalCouponRelation selectOne = personalCouponRelationMapper.selectOne(personalCouponRelation);
				if (selectOne == null) {
					personalCouponRelation.setPlatformId(marketingReceiveReq.getPlatformId());
					personalCouponRelation.setPersonalId(marketingReceiveReq.getPersonalId());
					personalCouponRelation.setMerchantId(marketingReceiveReq.getMerchantId());
					personalCouponRelation.setCouponId((String) couponInfo.get("couponId"));
					personalCouponRelation.setEffectiveStatus((String) couponInfo.get("effectiveStatus"));// 有效状态
					personalCouponRelation.setReceiveTime(new Date());
					personalCouponRelation.setUseStatus(CouponUseStatus.UNUSED.getCode());// 未使用
					personalCouponRelation.setSceneId((String) couponInfo.get("sceneId"));
					personalCouponRelationMapper.insertSelective(personalCouponRelation);
				}
			}
		}
		baseResponse.setCode(BizResponseCode.OK.getCode());
		baseResponse.setMessage(BizResponseCode.OK.getMessage());
		return baseResponse;
	}

	/**
	 * 积分领取 积分领取，查询用户积分账户是否存在，不存在创建账户并入账，存在直接入账，记录账务明细
	 * 
	 * @param marketingCouponReceiveReq
	 * @return
	 * @throws Exception 
	 */
	public BaseApiResponse integralReceive(MarketingReceiveReq marketingReceiveReq) throws Exception {
		BaseApiResponse baseResponse = new BaseApiResponse();
		BigDecimal integralAmount = new BigDecimal("0");
		if (!"".equals(marketingReceiveReq.getIntegralAmount()) && marketingReceiveReq.getIntegralAmount() != null) {
			integralAmount = new BigDecimal(marketingReceiveReq.getIntegralAmount());
		}
		IntoAccountRequest accountIntoRequest = new IntoAccountRequest();
		// String orderNo = SerialNoUtil.createID();
		// IntoAccountRequest intoAccountRequest = new IntoAccountRequest();
		// intoAccountRequest.setAccountType(AccountType.ACC_INTEGRAL.getCode());
		// intoAccountRequest.setCustomerType(CustomerTypeEnums.per_user.getCode());
		// intoAccountRequest.setIntoAccAmt(marketingReceiveReq.getIntegralAmount());
		// intoAccountRequest.setMerchantId(marketingReceiveReq.getMerchantId());
		// intoAccountRequest.setMerchantType(MerchantType.MER_PERSONAL.getCode());
		// intoAccountRequest.setOrderNo(orderNo);
		// intoAccountRequest.setPersonalId(marketingReceiveReq.getPersonalId());
		// intoAccountRequest.setPlatformId(marketingReceiveReq.getPlatformId());
		// intoAccountRequest.setTradeType(TradeType.RECEIVE.getCode());
		
		String accountId = "";
		String orderNo = SerialNoUtil.createID();
		accountIntoRequest.setOrderNo(orderNo);
		accountIntoRequest.setCustomerType(CustomerTypeEnums.CUST_PERSONAL.getCode());
		accountIntoRequest.setAccountType(AccountType.ACC_INTEGRAL.getCode());
		accountIntoRequest.setPersonalId(marketingReceiveReq.getPersonalId());
		accountIntoRequest.setPlatformId(marketingReceiveReq.getPlatformId());
		accountIntoRequest.setAccountType(AccountType.ACC_INTEGRAL.getCode());
		accountIntoRequest.setTradeType(TradeType.RECEIVE.getCode());
		accountIntoRequest.setMerchantType(MerchantType.MER_PERSONAL.getCode());
		accountIntoRequest.setIntoAccAmt(String.valueOf(integralAmount));
		accountIntoRequest.setMerchantId(marketingReceiveReq.getMerchantId());
		//查詢有木有賬戶
		BaseApiResponse accountQueryResponse = crmFeign.accountInfoQuery(accountIntoRequest);
		if (null == accountQueryResponse) {
			throw new BusinessException(BizResponseCode.INTEGRAL_RECEIVE_FAILED.getCode());
		}else {
			if (null != accountQueryResponse && BizResponseCode.OK.getCode().equals(accountQueryResponse.getCode())) {
				Map<String, Object> dataMap = (Map<String, Object>) accountQueryResponse.getData();
				accountId = (String) dataMap.get("accountId");
				accountIntoRequest.setAccountId(accountId);
			}else {
				if (BizResponseCode.MER_ACC_NOTEXIST.getCode().equals(accountQueryResponse.getCode()) || BizResponseCode.PER_ACC_NOTEXIST.getCode().equals(accountQueryResponse.getCode())) {
					//創建賬戶
					accountId = String.valueOf(SnowFlake.getId());
					accountIntoRequest.setAccountId(accountId);
					BaseApiResponse createResponse = crmFeign.createAccount(accountIntoRequest);
					if (!BizResponseCode.OK.getCode().equals(createResponse.getCode())) {
						baseResponse.setCode(createResponse.getCode());
						baseResponse.setMessage(createResponse.getMessage());
						return baseResponse;
					}
				}else {
					throw new BusinessException(BizResponseCode.INTEGRAL_RECEIVE_FAILED.getCode());
				}
			}
		}
		//入賬
		BaseApiResponse intoResponse = crmFeign.inAccountingTcc(accountIntoRequest);
		if (null != intoResponse && BizResponseCode.OK.getCode().equals(intoResponse.getCode())) {
			//如果是商户 余额账户
//			BaseApiResponse balIntoResponse = crmFeign.balAccountInto(accountIntoRequest);
//			if (null != balIntoResponse && BizResponseCode.OK.getCode().equals(balIntoResponse.getCode())) {
//				baseResponse.setCode(BizResponseCode.OK.getCode());
//				baseResponse.setMessage(BizResponseCode.OK.getMessage());
//			}else {
//				throw new BusinessException(BizResponseCode.INTEGRAL_RECEIVE_FAILED.getMessage());
//			}
			//存流水
			String journalId = String.valueOf(SnowFlake.getId());
			accountIntoRequest.setJournalId(journalId);
			accountIntoRequest.setOrderNo(orderNo);
			BaseApiResponse journalResponse = crmFeign.accountJournal(accountIntoRequest);
			if (null != journalResponse && BizResponseCode.OK.getCode().equals(journalResponse.getCode())) {
				baseResponse.setCode(BizResponseCode.OK.getCode());
				baseResponse.setMessage(BizResponseCode.OK.getMessage());
				baseResponse.setSuccess(true);
			}else {
				throw new BusinessException(BizResponseCode.INTEGRAL_RECEIVE_FAILED.getMessage());
			}
		}else {
			if (BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode().equals(intoResponse.getCode())) {
				baseResponse.setCode(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode());
				baseResponse.setMessage(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getMessage());
				return baseResponse;
			}
			if (BizResponseCode.MER_ACC_FREEZE.getCode().equals(intoResponse.getCode())) {
				baseResponse.setCode(BizResponseCode.MER_ACC_FREEZE.getCode());
				baseResponse.setMessage(BizResponseCode.MER_ACC_FREEZE.getMessage());
				return baseResponse;
			}
			if (BizResponseCode.REV_COUNT_OVER.getCode().equals(intoResponse.getCode())) {
				baseResponse.setCode(BizResponseCode.REV_COUNT_OVER.getCode());
				baseResponse.setMessage(BizResponseCode.REV_COUNT_OVER.getMessage());
				return baseResponse;
			}
			if (BizResponseCode.PER_NOTEXIST.getCode().equals(intoResponse.getCode())) {
				baseResponse.setCode(BizResponseCode.PER_NOTEXIST.getCode());
				baseResponse.setMessage(BizResponseCode.PER_NOTEXIST.getMessage());
				return baseResponse;
			}
			if (BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode().equals(intoResponse.getCode())) {
				baseResponse.setCode(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode());
				baseResponse.setMessage(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getMessage());
				return baseResponse;
			}
			if (BizResponseCode.PER_ACC_FREEZE.getCode().equals(intoResponse.getCode())) {
				baseResponse.setCode(BizResponseCode.PER_ACC_FREEZE.getCode());
				baseResponse.setMessage(BizResponseCode.PER_ACC_FREEZE.getMessage());
				return baseResponse;
			}
			throw new BusinessException(BizResponseCode.INTEGRAL_RECEIVE_FAILED.getMessage());
		}
		logger.info("积分领取结束{}"+baseResponse);
		return baseResponse;
	}

	/**
	 * 红包积分冻结整合方法 红包修改状态 积分冻结余额，记录明细和冻结流水
	 * 
	 * @param marketingFreezeReq
	 * @return
	 */
	@Hmily(confirmMethod="marketingFreezeConfirm",cancelMethod="marketingFreezeCancel")
	public BaseApiResponse marketingFreeze(MarketingFreezeReq marketingFreezeReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		Map<String, Object> beanToMap = EntityUtils.beanToMap(marketingFreezeReq);
		logger.info("红包积分冻结请求参数：{}" + beanToMap);
		// 校验用户信息
		Map<String, Object> reqMap = new HashMap<>();
		reqMap.put("platformId", marketingFreezeReq.getPlatformId());
		reqMap.put("personalId", marketingFreezeReq.getPersonalId());
		Map<String, Object> respMap = crmFeign.queryPersonalInfo(reqMap);

		if (null == respMap || !BizResponseCode.OK.getCode().equals(respMap.get("code"))) {
			baseResponse.setCode(BizResponseCode.PER_NOTEXIST.getCode());
			baseResponse.setMessage(BizResponseCode.PER_NOTEXIST.getMessage());
			return baseResponse;
		}
		if ("10".equals(marketingFreezeReq.getBusinessType())) {
			// 红包
			MarketingCouponFreezeReq marketingCouponFreezeReq = EntityUtils.map2Bean(beanToMap,
					MarketingCouponFreezeReq.class);
			baseResponse = couponFreeze(marketingCouponFreezeReq);
		} else if ("20".equals(marketingFreezeReq.getBusinessType())) {
			// 积分
			MarketingIntegralFreezeReq marketingIntegralFreezeReq = EntityUtils.map2Bean(beanToMap,
					MarketingIntegralFreezeReq.class);
			baseResponse = integralFreeze(marketingIntegralFreezeReq);
		} else if ("30".equals(marketingFreezeReq.getBusinessType())) {
			// 红包和积分
			// 红包
			MarketingCouponFreezeReq marketingCouponFreezeReq = EntityUtils.map2Bean(beanToMap,
					MarketingCouponFreezeReq.class);
			BaseApiResponse response = couponFreeze(marketingCouponFreezeReq);
			if (BizResponseCode.OK.getCode().equals(response.getCode())) {
				// 积分
				MarketingIntegralFreezeReq marketingIntegralFreezeReq = EntityUtils.map2Bean(beanToMap,
						MarketingIntegralFreezeReq.class);
				BaseApiResponse integralResponse = integralFreeze(marketingIntegralFreezeReq);
				if (BizResponseCode.OK.getCode().equals(integralResponse.getCode())) {
					baseResponse.setCode(BizResponseCode.OK.getCode());
					baseResponse.setMessage(BizResponseCode.OK.getMessage());
				} else {
					// 失败时解冻红包，返回失败
					PersonalCouponRelation PersonalCouponRelation = new PersonalCouponRelation();
					PersonalCouponRelation.setPlatformId(marketingCouponFreezeReq.getPlatformId());
					PersonalCouponRelation.setPersonalId(marketingCouponFreezeReq.getPersonalId());
					PersonalCouponRelation.setCouponSerialNo(marketingCouponFreezeReq.getCouponSerialNo());
					PersonalCouponRelation.setCouponId(marketingCouponFreezeReq.getCouponId());
					PersonalCouponRelation.setEffectiveStatus(EffectiveStatus.EFFECTIVE.getCode());// 有效
					PersonalCouponRelation selectOne = personalCouponRelationMapper.selectOne(PersonalCouponRelation);
					selectOne.setUseStatus(CouponUseStatus.UNUSED.getCode());// 未使用
					personalCouponRelationMapper.updateByPrimaryKey(selectOne);
					baseResponse.setCode(BizResponseCode.FREEZE_FAILED.getCode());
					baseResponse.setMessage(BizResponseCode.FREEZE_FAILED.getMessage());
				}
			} else {
				return response;
			}
		}
		logger.info("红包积分冻结响应参数：{}" + EntityUtils.beanToMap(baseResponse));
		return baseResponse;
	}

	
	public BaseApiResponse marketingFreezeConfirm(MarketingFreezeReq marketingFreezeReq){
		logger.info("--------marketingFreezeConfirm----------");
		return new BaseApiResponse();
	}
	
	public BaseApiResponse marketingFreezeCancel(MarketingFreezeReq marketingFreezeReq){
		logger.info("--------marketingFreezeCancel----------");
		return new BaseApiResponse();
	}
	/**
	 * 红包冻结 1、根据条件查询出红包 2、修改红包使用状态
	 * 
	 * @param marketingCouponFreezeReq
	 * @return
	 */
	public BaseApiResponse couponFreeze(MarketingCouponFreezeReq marketingCouponFreezeReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		// 查询可用红包是否存在
		PersonalCouponRelation PersonalCouponRelation = new PersonalCouponRelation();
		PersonalCouponRelation.setPlatformId(marketingCouponFreezeReq.getPlatformId());
		PersonalCouponRelation.setPersonalId(marketingCouponFreezeReq.getPersonalId());
		PersonalCouponRelation.setCouponSerialNo(marketingCouponFreezeReq.getCouponSerialNo());
		PersonalCouponRelation.setCouponId(marketingCouponFreezeReq.getCouponId());
		PersonalCouponRelation.setUseStatus(CouponUseStatus.UNUSED.getCode());// 未使用
		PersonalCouponRelation.setEffectiveStatus(EffectiveStatus.EFFECTIVE.getCode());// 有效
		PersonalCouponRelation selectOne = personalCouponRelationMapper.selectOne(PersonalCouponRelation);
		if (selectOne == null) {
			baseResponse.setCode(BizResponseCode.COUPON_IS_NULL.getCode());
			baseResponse.setMessage(BizResponseCode.COUPON_IS_NULL.getMessage());
		} else {
			// 修改红包使用状态
			selectOne.setUseStatus(CouponUseStatus.USING.getCode());// 使用中
			personalCouponRelationMapper.updateByPrimaryKey(selectOne);
			baseResponse.setCode(BizResponseCode.OK.getCode());
			baseResponse.setMessage(BizResponseCode.OK.getMessage());
		}
		return baseResponse;
	}

	/**
	 * 积分冻结 1、查询用户积分账户 2、积分账户余额出账，冻结余额增加 3、写入一条账务明细 4、写入一条冻结记录
	 * 
	 * @param marketingIntegralFreezeReq
	 * @return
	 */
	public BaseApiResponse integralFreeze(MarketingIntegralFreezeReq marketingIntegralFreezeReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		BigDecimal integralAmount = new BigDecimal(marketingIntegralFreezeReq.getIntegralAmount());
		FreezeRequest freezeRequest=new FreezeRequest();
		freezeRequest.setAccountType(AccountType.ACC_INTEGRAL.getCode());
		freezeRequest.setCustomerType(CustomerTypeEnums.CUST_PERSONAL.getCode());
		freezeRequest.setTradeType(TradeType.PAY.getCode());//01：支付，02：退款，03：提现，04：充值，05：预收款，06：消费，07：结算，08：领取
		freezeRequest.setMerchantType(MerchantType.MER_MERCHANT.getCode());
		freezeRequest.setMerchantId(marketingIntegralFreezeReq.getMerchantId());
		freezeRequest.setFreezeAmt(marketingIntegralFreezeReq.getIntegralAmount());
		freezeRequest.setOrderNo(marketingIntegralFreezeReq.getMerOrderNo());
		freezeRequest.setPersonalId(marketingIntegralFreezeReq.getPersonalId());
		freezeRequest.setPlatformId(marketingIntegralFreezeReq.getPlatformId());
		BaseApiResponse respMap = crmFeign.freezeTcc(freezeRequest);
		if (null != respMap && BizResponseCode.OK.getCode().equals(respMap.getCode())) {
			respMap = crmFeign.freezeJournalTcc(freezeRequest);
			if(null != respMap && BizResponseCode.OK.getCode().equals(respMap.getCode())){
				baseResponse.setCode(BizResponseCode.OK.getCode());
				baseResponse.setMessage(BizResponseCode.OK.getMessage());
			}else{
				throw new BusinessException(BizResponseCode.FREEZE_FAILED.getMessage());
			}
		}else if(respMap.getCode().equals(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode())
				||respMap.getCode().equals(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getCode())
				||respMap.getCode().equals(BizResponseCode.MER_ACC_NOTEXIST.getCode())
				||respMap.getCode().equals(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode())
				||respMap.getCode().equals(BizResponseCode.PER_ACC_BALANCE_NOT_ENOUGH.getCode())
				||respMap.getCode().equals(BizResponseCode.PER_ACC_NOTEXIST.getCode())){
			baseResponse.setCode(respMap.getCode());
			baseResponse.setMessage(respMap.getMessage());
		}else{
			throw new BusinessException(BizResponseCode.FREEZE_FAILED.getMessage());
		}
		return baseResponse;
	}

	/**
	 * 红包积分解冻整合方法 红包修改状态 积分解冻冻结余额，记录明细和冻结流水
	 * 
	 * @param marketingUnFreezeReq
	 * @return
	 */
	@Hmily(confirmMethod="marketingUnFreezeConfirm",cancelMethod="marketingUnFreezeCancel")
	public BaseApiResponse marketingUnFreeze(MarketingUnFreezeReq marketingUnFreezeReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		Map<String, Object> beanToMap = EntityUtils.beanToMap(marketingUnFreezeReq);
		logger.info("红包积分解冻请求参数：{}" + beanToMap);
		// 校验用户信息
		Map<String, Object> reqMap = new HashMap<>();
		reqMap.put("platformId", marketingUnFreezeReq.getPlatformId());
		reqMap.put("personalId", marketingUnFreezeReq.getPersonalId());
		Map<String, Object> respMap = crmFeign.queryPersonalInfo(reqMap);

		if (null == respMap || !BizResponseCode.OK.getCode().equals(respMap.get("code"))) {
			baseResponse.setCode(BizResponseCode.PER_NOTEXIST.getCode());
			baseResponse.setMessage(BizResponseCode.PER_NOTEXIST.getMessage());
			return baseResponse;
		}
		if ("10".equals(marketingUnFreezeReq.getBusinessType())) {
			// 红包
			MarketingCouponUnFreezeReq marketingCouponUnFreezeReq = EntityUtils.map2Bean(beanToMap,MarketingCouponUnFreezeReq.class);
			baseResponse = couponUnFreeze(marketingCouponUnFreezeReq);
		} else if ("20".equals(marketingUnFreezeReq.getBusinessType())) {
			// 积分
			MarketingIntegralUnFreezeReq marketingIntegralUnFreezeReq = EntityUtils.map2Bean(beanToMap,
					MarketingIntegralUnFreezeReq.class);
			baseResponse = integralUnFreeze(marketingIntegralUnFreezeReq);
		} else if ("30".equals(marketingUnFreezeReq.getBusinessType())) {
			// 红包和积分
			// 红包
			MarketingCouponUnFreezeReq marketingCouponUnFreezeReq = EntityUtils.map2Bean(beanToMap,
					MarketingCouponUnFreezeReq.class);
			BaseApiResponse response = couponUnFreeze(marketingCouponUnFreezeReq);
			if (BizResponseCode.OK.getCode().equals(response.getCode())) {
				// 积分
				MarketingIntegralUnFreezeReq marketingIntegralUnFreezeReq = EntityUtils.map2Bean(beanToMap,
						MarketingIntegralUnFreezeReq.class);
				BaseApiResponse integralResponse = integralUnFreeze(marketingIntegralUnFreezeReq);
				if (BizResponseCode.OK.getCode().equals(integralResponse.getCode())) {
					baseResponse.setCode(BizResponseCode.OK.getCode());
					baseResponse.setMessage(BizResponseCode.OK.getMessage());
				} else {
					// 失败时冻结红包，返回失败
					PersonalCouponRelation PersonalCouponRelation = new PersonalCouponRelation();
					PersonalCouponRelation.setPlatformId(marketingCouponUnFreezeReq.getPlatformId());
					PersonalCouponRelation.setPersonalId(marketingCouponUnFreezeReq.getPersonalId());
					PersonalCouponRelation.setCouponSerialNo(marketingCouponUnFreezeReq.getCouponSerialNo());
					PersonalCouponRelation.setCouponId(marketingCouponUnFreezeReq.getCouponId());
					PersonalCouponRelation.setEffectiveStatus(EffectiveStatus.EFFECTIVE.getCode());// 有效
					PersonalCouponRelation selectOne = personalCouponRelationMapper.selectOne(PersonalCouponRelation);
					selectOne.setUseStatus(CouponUseStatus.USING.getCode());// 使用中
					personalCouponRelationMapper.updateByPrimaryKey(selectOne);
					baseResponse.setCode(BizResponseCode.UNFREEZE_FAILED.getCode());
					baseResponse.setMessage(BizResponseCode.UNFREEZE_FAILED.getMessage());
				}
			} else {
				return response;
			}
		}
		logger.info("红包积分解冻响应参数：{}" + EntityUtils.beanToMap(baseResponse));
		return baseResponse;
	}
	
	public BaseApiResponse marketingUnFreezeConfirm(MarketingUnFreezeReq marketingUnFreezeReq){
		logger.info("--------marketingUnFreezeConfirm----------");
		return new BaseApiResponse();
	}
	
	public BaseApiResponse marketingUnFreezeCancel(MarketingUnFreezeReq marketingUnFreezeReq){
		logger.info("--------marketingUnFreezeCancel----------");
		return new BaseApiResponse();
	}

	/**
	 * 红包解冻 1、根据条件查询出红包 2、修改红包使用状态
	 * 
	 * @param marketingCouponUnFreezeReq
	 * @return
	 */
	public BaseApiResponse couponUnFreeze(MarketingCouponUnFreezeReq marketingCouponUnFreezeReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		// 查询可用红包是否存在
		PersonalCouponRelation PersonalCouponRelation = new PersonalCouponRelation();
		PersonalCouponRelation.setPlatformId(marketingCouponUnFreezeReq.getPlatformId());
		PersonalCouponRelation.setPersonalId(marketingCouponUnFreezeReq.getPersonalId());
		PersonalCouponRelation.setCouponSerialNo(marketingCouponUnFreezeReq.getCouponSerialNo());
		PersonalCouponRelation.setCouponId(marketingCouponUnFreezeReq.getCouponId());
		PersonalCouponRelation.setUseStatus(CouponUseStatus.USING.getCode());// 使用中
		PersonalCouponRelation.setEffectiveStatus(EffectiveStatus.EFFECTIVE.getCode());// 有效
		PersonalCouponRelation selectOne = personalCouponRelationMapper.selectOne(PersonalCouponRelation);
		if (selectOne == null) {
			baseResponse.setCode(BizResponseCode.COUPON_IS_NULL.getCode());
			baseResponse.setMessage(BizResponseCode.COUPON_IS_NULL.getMessage());
		} else {
			// 修改红包使用状态
			selectOne.setUseStatus(CouponUseStatus.UNUSED.getCode());// 未使用
			personalCouponRelationMapper.updateByPrimaryKey(selectOne);
			baseResponse.setCode(BizResponseCode.OK.getCode());
			baseResponse.setMessage(BizResponseCode.OK.getMessage());
		}
		return baseResponse;
	}

	/**
	 * 积分解冻 1、查询用户积分账户冻结金额 2、积分账户余额入账，冻结余额减少 3、写入一条账务明细 4、写入一条冻结记录
	 * 
	 * @param marketingIntegralUnFreezeReq
	 * @return
	 */
	public BaseApiResponse integralUnFreeze(MarketingIntegralUnFreezeReq marketingIntegralUnFreezeReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		BigDecimal integralAmount = new BigDecimal(marketingIntegralUnFreezeReq.getIntegralAmount());
		UnFreezeRequest unFreezeRequest=new UnFreezeRequest();
		unFreezeRequest.setAccountType(AccountType.ACC_INTEGRAL.getCode());
		unFreezeRequest.setCustomerType(CustomerTypeEnums.CUST_PERSONAL.getCode());
		unFreezeRequest.setFreezeId(marketingIntegralUnFreezeReq.getFreezeNoRel());
		unFreezeRequest.setMerchantId(marketingIntegralUnFreezeReq.getMerchantId());
		unFreezeRequest.setMerchantType(MerchantType.MER_MERCHANT.getCode());
		unFreezeRequest.setOrderNo(marketingIntegralUnFreezeReq.getMerOrderNo());
		unFreezeRequest.setPersonalId(marketingIntegralUnFreezeReq.getPersonalId());
		unFreezeRequest.setPlatformId(marketingIntegralUnFreezeReq.getPlatformId());
		unFreezeRequest.setTradeType(TradeType.PAY.getCode());//01：支付，02：退款，03：提现，04：充值，05：预收款，06：消费，07：结算，08：领取
		unFreezeRequest.setUnfreezeAmt(marketingIntegralUnFreezeReq.getIntegralAmount());
		BaseApiResponse respMap=crmFeign.verificationFreezeId(unFreezeRequest);//校验冻结流水是否已经被解冻
		if(null != respMap && BizResponseCode.OK.getCode().equals(respMap.getCode())){
			respMap= crmFeign.unfreezeTcc(unFreezeRequest);
			if (null != respMap && BizResponseCode.OK.getCode().equals(respMap.getCode())) {
				respMap= crmFeign.unfreezeJournalTcc(unFreezeRequest);
				if(null != respMap && BizResponseCode.OK.getCode().equals(respMap.getCode())){
					baseResponse.setCode(BizResponseCode.OK.getCode());
					baseResponse.setMessage(BizResponseCode.OK.getMessage());
				}else{
					throw new BusinessException(BizResponseCode.UNFREEZE_FAILED.getMessage());
				}
			}else if(respMap.getCode().equals(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode())
					||respMap.getCode().equals(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getCode())
					||respMap.getCode().equals(BizResponseCode.MER_ACC_NOTEXIST.getCode())
					||respMap.getCode().equals(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode())
					||respMap.getCode().equals(BizResponseCode.PER_ACC_BALANCE_NOT_ENOUGH.getCode())
					||respMap.getCode().equals(BizResponseCode.PER_ACC_NOTEXIST.getCode())){
				baseResponse.setCode(respMap.getCode());
				baseResponse.setMessage(respMap.getMessage());
			}else{
				throw new BusinessException(BizResponseCode.UNFREEZE_FAILED.getMessage());
			}
		}else{
			baseResponse.setCode(respMap.getCode());
			baseResponse.setMessage(respMap.getMessage());
		}
		return baseResponse;
	}

	/**
	 * 红包积分解冻并出账整合方法 红包修改状态 积分解冻冻结余额，记录明细和冻结流水
	 * 
	 * @param marketingUnFreezeAndPayReq
	 * @return
	 */
	@Hmily(confirmMethod="marketingUnFreezeAndPayConfirm",cancelMethod="marketingUnFreezeAndPayCancel")
	public BaseApiResponse marketingUnFreezeAndPay(MarketingUnFreezeAndPayReq marketingUnFreezeAndPayReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		Map<String, Object> beanToMap = EntityUtils.beanToMap(marketingUnFreezeAndPayReq);
		logger.info("红包积分解冻请求参数：{}" + beanToMap);
		// 校验用户信息
		Map<String, Object> reqMap = new HashMap<>();
		reqMap.put("platformId", marketingUnFreezeAndPayReq.getPlatformId());
		reqMap.put("personalId", marketingUnFreezeAndPayReq.getPersonalId());
		Map<String, Object> respMap = crmFeign.queryPersonalInfo(reqMap);

		if (null == respMap || !BizResponseCode.OK.getCode().equals(respMap.get("code"))) {
			baseResponse.setCode(BizResponseCode.PER_NOTEXIST.getCode());
			baseResponse.setMessage(BizResponseCode.PER_NOTEXIST.getMessage());
			return baseResponse;
		}
		if ("10".equals(marketingUnFreezeAndPayReq.getBusinessType())) {
			// 红包
			MarketingCouponUnFreezeAndPayReq marketingCouponUnFreezeAndPayReq = EntityUtils.map2Bean(beanToMap,
					MarketingCouponUnFreezeAndPayReq.class);
			baseResponse = couponUnFreezeAndPay(marketingCouponUnFreezeAndPayReq);
		} else if ("20".equals(marketingUnFreezeAndPayReq.getBusinessType())) {
			// 积分
			MarketingIntegralUnFreezeAndPayReq marketingIntegralUnFreezeAndPayReq = EntityUtils.map2Bean(beanToMap,
					MarketingIntegralUnFreezeAndPayReq.class);
			baseResponse = integralUnFreezeAndPay(marketingIntegralUnFreezeAndPayReq);
		} else if ("30".equals(marketingUnFreezeAndPayReq.getBusinessType())) {
			// 红包和积分
			// 红包
			MarketingCouponUnFreezeAndPayReq marketingCouponUnFreezeAndPayReq = EntityUtils.map2Bean(beanToMap,
					MarketingCouponUnFreezeAndPayReq.class);
			BaseApiResponse response = couponUnFreezeAndPay(marketingCouponUnFreezeAndPayReq);
			if (BizResponseCode.OK.getCode().equals(response.getCode())) {
				// 积分
				MarketingIntegralUnFreezeAndPayReq marketingIntegralUnFreezeAndPayReq = EntityUtils.map2Bean(beanToMap,
						MarketingIntegralUnFreezeAndPayReq.class);
				BaseApiResponse integralResponse = integralUnFreezeAndPay(marketingIntegralUnFreezeAndPayReq);
				if (BizResponseCode.OK.getCode().equals(integralResponse.getCode())) {
					baseResponse.setCode(BizResponseCode.OK.getCode());
					baseResponse.setMessage(BizResponseCode.OK.getMessage());
				} else {
					// 失败时冻结红包，返回失败
					PersonalCouponRelation personalCouponRelation = new PersonalCouponRelation();
					personalCouponRelation.setPlatformId(marketingCouponUnFreezeAndPayReq.getPlatformId());
					personalCouponRelation.setPersonalId(marketingCouponUnFreezeAndPayReq.getPersonalId());
					personalCouponRelation.setCouponSerialNo(marketingCouponUnFreezeAndPayReq.getCouponSerialNo());
					personalCouponRelation.setCouponId(marketingCouponUnFreezeAndPayReq.getCouponId());
					personalCouponRelation.setEffectiveStatus(EffectiveStatus.EFFECTIVE.getCode());// 有效
					PersonalCouponRelation selectOne = personalCouponRelationMapper.selectOne(personalCouponRelation);
					selectOne.setUseStatus(CouponUseStatus.USING.getCode());// 使用中
					personalCouponRelationMapper.updateByPrimaryKey(selectOne);
					baseResponse.setCode(BizResponseCode.UNFREEZE_FAILED.getCode());
					baseResponse.setMessage(BizResponseCode.UNFREEZE_FAILED.getMessage());
				}
			} else {
				return response;
			}
		}
		logger.info("红包积分解冻响应参数：{}" + EntityUtils.beanToMap(baseResponse));
		if (BizResponseCode.OK.getCode().equals(baseResponse.getCode())) {
			// 成功后调用营销引擎记录流水
			CouponIntegralUseReq couponIntegralUseReq = new CouponIntegralUseReq();
			couponIntegralUseReq.setPlatformId(marketingUnFreezeAndPayReq.getPlatformId());
			couponIntegralUseReq.setPersonalId(marketingUnFreezeAndPayReq.getPersonalId());
			couponIntegralUseReq.setMerchantId(marketingUnFreezeAndPayReq.getMerchantId());
			couponIntegralUseReq.setCouponId(marketingUnFreezeAndPayReq.getCouponId());
			couponIntegralUseReq.setCouponSerialNo(marketingUnFreezeAndPayReq.getCouponSerialNo());
			couponIntegralUseReq.setSceneId(marketingUnFreezeAndPayReq.getSceneId());
			couponIntegralUseReq.setIntegralAmount(marketingUnFreezeAndPayReq.getIntegralAmount());
			couponIntegralUseReq.setMerOrderNo(marketingUnFreezeAndPayReq.getMerOrderNo());
			couponIntegralUseReq.setCouponAmt(marketingUnFreezeAndPayReq.getCouponAmt());
			couponIntegralUseReq.setIntegralAmt(marketingUnFreezeAndPayReq.getIntegralAmt());
			logger.info("调用营销引擎消费请求参数：{}" + couponIntegralUseReq);
			codexEngineBiz.consume(couponIntegralUseReq);
		}
		return baseResponse;
	}

	public BaseApiResponse marketingUnFreezeAndPayConfirm(MarketingUnFreezeAndPayReq marketingUnFreezeAndPayReq){
		logger.info("--------marketingUnFreezeAndPayConfirm----------");
		return new BaseApiResponse();
	}
	
	public BaseApiResponse marketingUnFreezeAndPayCancel(MarketingUnFreezeAndPayReq marketingUnFreezeAndPayReq){
		logger.info("--------marketingUnFreezeAndPayCancel----------");
		return new BaseApiResponse();
	}
	
	/**
	 * 红包解冻并出账 1、根据条件查询出红包 2、修改红包使用状态
	 * 
	 * @param marketingCouponFreezeReq
	 * @return
	 */
	public BaseApiResponse couponUnFreezeAndPay(MarketingCouponUnFreezeAndPayReq marketingCouponUnFreezeAndPayReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		// 查询可用红包是否存在
		PersonalCouponRelation PersonalCouponRelation = new PersonalCouponRelation();
		PersonalCouponRelation.setPlatformId(marketingCouponUnFreezeAndPayReq.getPlatformId());
		PersonalCouponRelation.setPersonalId(marketingCouponUnFreezeAndPayReq.getPersonalId());
		PersonalCouponRelation.setCouponSerialNo(marketingCouponUnFreezeAndPayReq.getCouponSerialNo());
		PersonalCouponRelation.setCouponId(marketingCouponUnFreezeAndPayReq.getCouponId());
		PersonalCouponRelation.setUseStatus(CouponUseStatus.USING.getCode());// 使用中
		PersonalCouponRelation.setEffectiveStatus(EffectiveStatus.EFFECTIVE.getCode());// 有效
		PersonalCouponRelation selectOne = personalCouponRelationMapper.selectOne(PersonalCouponRelation);
		if (selectOne == null) {
			baseResponse.setCode(BizResponseCode.COUPON_IS_NULL.getCode());
			baseResponse.setMessage(BizResponseCode.COUPON_IS_NULL.getMessage());
		} else {
			// 修改红包使用状态
			selectOne.setUseStatus(CouponUseStatus.USED.getCode());// 已使用
			personalCouponRelationMapper.updateByPrimaryKey(selectOne);
			baseResponse.setCode(BizResponseCode.OK.getCode());
			baseResponse.setMessage(BizResponseCode.OK.getMessage());
		}
		return baseResponse;
	}

	/**
	 * 积分解冻并出账 1、查询用户积分账户冻结金额 2、积分账户冻结余额减少 3、写入一条账务明细 4、写入一条冻结记录
	 * 
	 * @param marketingIntegralUnFreezeReq
	 * @return
	 */
	public BaseApiResponse integralUnFreezeAndPay(MarketingIntegralUnFreezeAndPayReq marketingIntegralUnFreezeAndPayReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		BigDecimal integralAmount = new BigDecimal(marketingIntegralUnFreezeAndPayReq.getIntegralAmount());
		
		
		UnFreezeAndOutAccountRequest unFreAndOutAccountRequest = new UnFreezeAndOutAccountRequest();
		unFreAndOutAccountRequest.setPlatformId(marketingIntegralUnFreezeAndPayReq.getPlatformId());
		unFreAndOutAccountRequest.setUnFreezeAmt(marketingIntegralUnFreezeAndPayReq.getIntegralAmount());
		unFreAndOutAccountRequest.setMerchantId(marketingIntegralUnFreezeAndPayReq.getMerchantId());
		unFreAndOutAccountRequest.setMerchantType(MerchantType.MER_MERCHANT.getCode());
		unFreAndOutAccountRequest.setCustomerType(CustomerTypeEnums.CUST_PERSONAL.getCode());
		unFreAndOutAccountRequest.setAccountType(AccountType.ACC_INTEGRAL.getCode());
		unFreAndOutAccountRequest.setFreezeId(marketingIntegralUnFreezeAndPayReq.getFreezeNoRel());
		unFreAndOutAccountRequest.setOrderNo(marketingIntegralUnFreezeAndPayReq.getMerOrderNo());
		unFreAndOutAccountRequest.setPersonalId(marketingIntegralUnFreezeAndPayReq.getPersonalId());
		unFreAndOutAccountRequest.setTradeType(TradeType.PAY.getCode());//01：支付，02：退款，03：提现，04：充值，05：预收款，06：消费，07：结算，08：领取
		
		BaseApiResponse respMap=crmFeign.verificationFreeze(unFreAndOutAccountRequest);//校验冻结流水是否已经被解冻
		if(null != respMap && BizResponseCode.OK.getCode().equals(respMap.getCode())){
			respMap= crmFeign.unFreAndOutAccountTcc(unFreAndOutAccountRequest);
			if (null != respMap && BizResponseCode.OK.getCode().equals(respMap.getCode())) {
				String accountId = (String)respMap.getData();
				unFreAndOutAccountRequest.setAccountId(accountId);
				respMap= crmFeign.insertPerActJournalTcc(unFreAndOutAccountRequest);
				if (null != respMap && BizResponseCode.OK.getCode().equals(respMap.getCode())) {
					respMap= crmFeign.unFreAndOutJournalTcc(unFreAndOutAccountRequest);
					if (null != respMap && BizResponseCode.OK.getCode().equals(respMap.getCode())) {
						baseResponse.setCode(BizResponseCode.OK.getCode());
						baseResponse.setMessage(BizResponseCode.OK.getMessage());
					}else{
						throw new BusinessException(BizResponseCode.UNFREEZE_FAILED.getMessage());
					}
				}else{
					throw new BusinessException(BizResponseCode.UNFREEZE_FAILED.getMessage());
				}
			}else if (respMap.getCode().equals(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode())
					||respMap.getCode().equals(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getCode())
					||respMap.getCode().equals(BizResponseCode.MER_ACC_NOTEXIST.getCode())
					||respMap.getCode().equals(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode())
					||respMap.getCode().equals(BizResponseCode.PER_ACC_NOTEXIST.getCode())
					||respMap.getCode().equals(BizResponseCode.PER_ACC_NOTEXIST.getCode())){
				baseResponse.setCode(respMap.getCode());
				baseResponse.setMessage(respMap.getMessage());
				
			}else{
				baseResponse.setCode(BizResponseCode.UNFREEZE_FAILED.getCode());
				baseResponse.setMessage(BizResponseCode.UNFREEZE_FAILED.getMessage());
			}
		}
		baseResponse.setCode(respMap.getCode());
		baseResponse.setMessage(respMap.getMessage());
		return baseResponse;
	}

	/**
	 * 红包、积分退款整合方法
	 * 
	 * @param marketingRefundReq
	 * @return
	 */
	public BaseApiResponse marketingRefund(MarketingRefundReq marketingRefundReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		Map<String, Object> beanToMap = EntityUtils.beanToMap(marketingRefundReq);
		logger.info("红包、积分退款整合方法请求参数：{}" + beanToMap);
		// 校验用户信息
		Map<String, Object> reqMap = new HashMap<>();
		reqMap.put("platformId", marketingRefundReq.getPlatformId());
		reqMap.put("personalId", marketingRefundReq.getPersonalId());
		Map<String, Object> respMap = crmFeign.queryPersonalInfo(reqMap);

		if (null == respMap || !BizResponseCode.OK.getCode().equals(respMap.get("code"))) {
			baseResponse.setCode(BizResponseCode.PER_NOTEXIST.getCode());
			baseResponse.setMessage(BizResponseCode.PER_NOTEXIST.getMessage());
			return baseResponse;
		}
		if ("20".equals(marketingRefundReq.getRefundType())) {
			// 只退积分
			MarketingIntegralRefundReq marketingIntegralRefundReq = EntityUtils.map2Bean(beanToMap,
					MarketingIntegralRefundReq.class);
			baseResponse = integralRefund(marketingIntegralRefundReq);
		} else if ("10".equals(marketingRefundReq.getRefundType()) || "30".equals(marketingRefundReq.getRefundType())) {
			// 退红包和积分
			// 红包
			MarketingCouponRefundReq marketingCouponRefundReq = EntityUtils.map2Bean(beanToMap,
					MarketingCouponRefundReq.class);
			BaseApiResponse response = couponRefund(marketingCouponRefundReq);
			if (BizResponseCode.OK.getCode().equals(response.getCode())) {
				// 积分
				MarketingIntegralRefundReq marketingIntegralRefundReq = EntityUtils.map2Bean(beanToMap,
						MarketingIntegralRefundReq.class);
				BaseApiResponse integralResponse = integralRefund(marketingIntegralRefundReq);
				if (BizResponseCode.OK.getCode().equals(integralResponse.getCode())) {
					baseResponse.setCode(BizResponseCode.OK.getCode());
					baseResponse.setMessage(BizResponseCode.OK.getMessage());
				} else {
					// 失败时红包状态还原为已使用，返回失败
					PersonalCouponRelation PersonalCouponRelation = new PersonalCouponRelation();
					PersonalCouponRelation.setPlatformId(marketingCouponRefundReq.getPlatformId());
					PersonalCouponRelation.setPersonalId(marketingCouponRefundReq.getPersonalId());
					PersonalCouponRelation.setCouponSerialNo(marketingCouponRefundReq.getCouponSerialNo());
					PersonalCouponRelation.setCouponId(marketingCouponRefundReq.getCouponId());
					PersonalCouponRelation.setEffectiveStatus(EffectiveStatus.EFFECTIVE.getCode());// 有效
					PersonalCouponRelation selectOne = personalCouponRelationMapper.selectOne(PersonalCouponRelation);
					selectOne.setUseStatus(CouponUseStatus.USED.getCode());// 已使用
					personalCouponRelationMapper.updateByPrimaryKey(selectOne);
					baseResponse.setCode(BizResponseCode.UNFREEZE_FAILED.getCode());
					baseResponse.setMessage(BizResponseCode.UNFREEZE_FAILED.getMessage());
				}
			} else {
				return response;
			}
		}
		logger.info("红包、积分退款整合方法响应参数：{}" + EntityUtils.beanToMap(baseResponse));
		if (BizResponseCode.OK.getCode().equals(baseResponse.getCode())) {
			// 成功后调用营销引擎记录流水
			CouponIntegralRefundReq couponIntegralRefundReq = new CouponIntegralRefundReq();
			couponIntegralRefundReq.setPlatformId(marketingRefundReq.getPlatformId());
			couponIntegralRefundReq.setMerOrderNo(marketingRefundReq.getMerOrderNo());
			codexEngineBiz.refund(couponIntegralRefundReq);
		}
		return baseResponse;
	}

	/**
	 * 红包退款 1、根据条件查询出红包 2、修改红包使用状态
	 * 
	 * @param marketingCouponRefundReq
	 * @return
	 */
	public BaseApiResponse couponRefund(MarketingCouponRefundReq marketingCouponRefundReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		// 查询可用红包是否存在
		PersonalCouponRelation PersonalCouponRelation = new PersonalCouponRelation();
		PersonalCouponRelation.setPlatformId(marketingCouponRefundReq.getPlatformId());
		PersonalCouponRelation.setPersonalId(marketingCouponRefundReq.getPersonalId());
		PersonalCouponRelation.setCouponSerialNo(marketingCouponRefundReq.getCouponSerialNo());
		PersonalCouponRelation.setCouponId(marketingCouponRefundReq.getCouponId());
		// 存在冻结状态直接退款，暂时注释
		// PersonalCouponRelation.setUseStatus(CouponUseStatus.USED.getCode());//已使用
		PersonalCouponRelation.setEffectiveStatus(EffectiveStatus.EFFECTIVE.getCode());// 有效
		PersonalCouponRelation selectOne = personalCouponRelationMapper.selectOne(PersonalCouponRelation);
		if (selectOne == null) {
			baseResponse.setCode(BizResponseCode.COUPON_IS_NULL.getCode());
			baseResponse.setMessage(BizResponseCode.COUPON_IS_NULL.getMessage());
		} else {
			// 修改红包使用状态
			selectOne.setUseStatus(CouponUseStatus.UNUSED.getCode());// 未使用
			personalCouponRelationMapper.updateByPrimaryKey(selectOne);
			baseResponse.setCode(BizResponseCode.OK.getCode());
			baseResponse.setMessage(BizResponseCode.OK.getMessage());
		}
		return baseResponse;
	}

	/**
	 * 积分退款 1、查询用户积分余额 2、积分账户余额增加 3、写入一条账务明细
	 * 
	 * @param marketingIntegralRefundReq
	 * @return
	 */
	public BaseApiResponse integralRefund(MarketingIntegralRefundReq marketingIntegralRefundReq) {
		BaseApiResponse baseResponse = new BaseApiResponse();
		String amount = marketingIntegralRefundReq.getIntegralAmount();
		
		
		Map<String, Object> reqMap = new HashMap<String, Object>();
		reqMap.put("platformId", marketingIntegralRefundReq.getPlatformId());
		reqMap.put("orderNo", marketingIntegralRefundReq.getMerOrderNo());
		reqMap.put("intoAccAmt", marketingIntegralRefundReq.getIntegralAmount());
		reqMap.put("merchantId", marketingIntegralRefundReq.getMerchantId());
		reqMap.put("merchantType", MerchantType.MER_PERSONAL.getCode());
		reqMap.put("customerType", CustomerTypeEnums.CUST_PERSONAL.getCode());
		reqMap.put("accountType", AccountType.ACC_INTEGRAL.getCode());
		reqMap.put("personalId", marketingIntegralRefundReq.getPersonalId());
		reqMap.put("tradeType", TradeType.RECEIVE.getCode());//01：支付，02：退款，03：提现，04：充值，05：预收款，06：消费，07：结算，08：领取
		Map<String, Object> reqspMap = crmFeign.inAccounting(reqMap);
		if (null != reqspMap && BizResponseCode.OK.getCode().equals(reqspMap.get("code"))) {
			baseResponse.setCode(BizResponseCode.OK.getCode());
			baseResponse.setMessage(BizResponseCode.OK.getMessage());
		} else {
			baseResponse.setCode(BizResponseCode.INTEGRAL_REFUND_SAVE_FAILED.getCode());
			baseResponse.setMessage(BizResponseCode.INTEGRAL_REFUND_SAVE_FAILED.getMessage());
		}
		
		return baseResponse;
	}

	/**
	 * 积分明细列表查询
	 * 
	 * @param params
	 * @return
	 */
	public MarketingIntegralDetailListRsp integralDetail(MarketingIntegralDetailListReq params) {
		MarketingIntegralDetailListRsp marketingIntegralDetailListRsp = new MarketingIntegralDetailListRsp();
		Map<String, Object> reqMap = new HashMap<String, Object>();
		String platformId = params.getPlatformId();
		String personalId = params.getPersonalId();
		reqMap.put("platformId", platformId);
//		reqMap.put("merchantId", marketingIntegralRefundReq.getMerchantId());
//		reqMap.put("merchantType", MerchantType.MER_PERSONAL.getCode());
		reqMap.put("customerType", CustomerTypeEnums.CUST_PERSONAL.getCode());
		reqMap.put("accountType", AccountType.ACC_INTEGRAL.getCode());
		reqMap.put("personalId", personalId);
		Map<String, Object> respMap = crmFeign.accountQuery(reqMap);
		if (null != respMap && BizResponseCode.OK.getCode().equals(respMap.get("code"))) {
			PersonalAccountEntity personalAccountEntity=null;
			if(respMap.get("accountInfo")!=null){
				Map<String,Object> accountInfoMap=(HashMap<String,Object>)respMap.get("accountInfo");
				personalAccountEntity=new PersonalAccountEntity();
				try {
					if(accountInfoMap.get("createTime")!=null){
						Date createTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(accountInfoMap.get("createTime")==null?"":accountInfoMap.get("createTime").toString());
						accountInfoMap.put("createTime", createTime);
					}
				} catch (ParseException e) {
					e.printStackTrace();
					accountInfoMap.remove("createTime");
					
				}
				try {
					if(accountInfoMap.get("updateTime")!=null){
						Date updateTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(accountInfoMap.get("updateTime")==null?"":accountInfoMap.get("updateTime").toString());
						accountInfoMap.put("updateTime", updateTime);
					}
				} catch (ParseException e) {
					e.printStackTrace();
					accountInfoMap.remove("updateTime");
					
				}				
				personalAccountEntity=EntityUtils.map2Bean(accountInfoMap,PersonalAccountEntity.class);
			}
			if (null != personalAccountEntity) {
				marketingIntegralDetailListRsp.setBalance(personalAccountEntity.getAccountBalance().setScale(0).toString());
			} else {
				marketingIntegralDetailListRsp.setBalance("0");
			}
			reqMap.put("platformId", platformId);
			reqMap.put("personalId", personalId);
			reqMap.put("accountType", AccountType.ACC_INTEGRAL.getCode());
			Map<String, Object> respJournalMap = crmFeign.accountJournalQuery(reqMap);
			
			if (null != respJournalMap && BizResponseCode.OK.getCode().equals(respJournalMap.get("code"))) {
				marketingIntegralDetailListRsp.setIntegralDetailList(respJournalMap.get("accountJournalInfo"));
			}
			
			marketingIntegralDetailListRsp.setCode(BizResponseCode.OK.getCode());
			marketingIntegralDetailListRsp.setMessage(BizResponseCode.OK.getMessage());
		} else {
			marketingIntegralDetailListRsp.setCode(BizResponseCode.PER_ACC_NOTEXIST.getCode());
			marketingIntegralDetailListRsp.setMessage(BizResponseCode.PER_ACC_NOTEXIST.getMessage());
		}
		return marketingIntegralDetailListRsp;
	}
}